package com.zttek.photosShareProject.modules.service.share.impl;

import com.zttek.photosShareProject.common.myException.DBException;
import com.zttek.photosShareProject.modules.mapper.AbuseMapper;
import com.zttek.photosShareProject.modules.model.Abuse;
import com.zttek.photosShareProject.modules.service.share.IAbuseService;
import com.zttek.photosShareProject.modules.service.share.IExamineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Y
 * Date: 2017-08-19
 * Time: 16:32
 */

@Service
public class AbuseService implements IAbuseService {

    @Autowired
    private AbuseMapper abuseMapper;

    @Autowired
    private IExamineService examineService;

    @Transactional
    @Override
    public void abuseShare(long targetId, long informerId, String reason) throws Exception {
        Date date = new Date();
        Abuse abuse = fill(targetId, SHARE, reason, date);
        abuse.setInfomerId(informerId);
//        abuseInsert(abuse);
        abuseMapper.insert(abuse);

        examineService.queueAbuse(targetId, date);
    }

    @Transactional
    @Override
    public void abuseComment(long targetId, long informerId, String reason) throws Exception {
        Date date = new Date();
        Abuse abuse = fill(targetId, COMMENT, reason, date);
        abuse.setInfomerId(informerId);
//        abuseInsert(abuse);
        abuseMapper.insert(abuse);

        examineService.queueAbuse(targetId, date);
    }

    @Override
    public Abuse selectAbuse(Long abuseId) throws Exception {
//        Abuse abuse = abuseSelectById(abuseId);
        Abuse abuse = abuseMapper.selectById(abuseId);
        return abuse;
    }

    @Transactional
    @Override
    public void handle(Long abuseId, boolean isPassed) throws Exception {
//        Abuse abuse = abuseSelectById(abuseId);
        Abuse abuse = abuseMapper.selectById(abuseId);
        int result = 1;
        if (!isPassed)
            result = 2;
        abuse.setResult(result);
        abuse.setFinishTime(new Date());

//        abuseUpdate(abuse);
        abuseMapper.updateById(abuse);
    }


    private Abuse fill(long targetId, int type, String reason, Date date) {
        Abuse abuse = new Abuse();
        abuse.setReason(reason);
        abuse.setTargetId(targetId);
        abuse.setCreateTime(date);
        abuse.setType(type);

        return abuse;
    }

    @Deprecated
    private Abuse abuseSelectById(long abuseId){
        Abuse abuse = null;
        try {
            abuse = abuseMapper.selectById(abuseId);
        }catch (Exception e){
            e.printStackTrace();
            throw new DBException(DBException.ABUSE_SELECT_FAILURE);
        }
        return abuse;
    }

    @Deprecated
    private void abuseInsert(Abuse abuse){
        try {
            abuseMapper.insert(abuse);
        }catch (Exception e){
            e.printStackTrace();
            throw new DBException(DBException.ABUSE_INSERT_FAILURE);
        }
    }

    @Deprecated
    private void abuseUpdate(Abuse abuse){
        try {
            abuseMapper.updateById(abuse);
        }catch (Exception e){
            e.printStackTrace();
            throw new DBException(DBException.ABUSE_UPDATE_FAILURE);
        }
    }
}
